package com.hope.onlinestudy.activity.start

import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.KeyEvent
import android.view.View
import android.widget.RadioGroup
import com.hope.onlinestudy.R
import com.hope.onlinestudy.adapter.FtPagerAdapter
import com.hope.onlinestudy.fragment.user.UserFragment
import com.hope.onlinestudy.fragment.web.WebFragment
import com.hope.onlinestudy.mvp.MVPBaseActivity
import com.hope.onlinestudy.utils.ApiUtils
import kotlinx.android.synthetic.main.activity_start.*
import org.jetbrains.anko.toast

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */
class StartActivity :
    MVPBaseActivity<StartContract.View, StartPresenter>(), ViewPager.OnPageChangeListener,
    RadioGroup.OnCheckedChangeListener,
    StartContract.View {

    private val homeFragment: WebFragment by lazy {
        WebFragment.instance(
            ApiUtils.toAppHomePage,
            true
        )
    }
    private val typeFragment by lazy { WebFragment.instance(ApiUtils.toTypeList) }
    private val examFragment by lazy { WebFragment.instance(ApiUtils.toAppExam) }

    override fun getLayoutView(): Int {
        return R.layout.activity_start
    }

    override fun initData() {
        val fmList = arrayListOf<Fragment>()
        fmList.add(homeFragment)
        fmList.add(typeFragment)
        fmList.add(examFragment)
        fmList.add(UserFragment())

        val adapter = FtPagerAdapter(supportFragmentManager, fmList)
        vfpMain.adapter = adapter
        vfpMain.setOnPageChangeListener(this)
        gr_bottom.setOnCheckedChangeListener(this)

    }

    fun hideNavgiationBar(visit: Boolean) {
        if (visit) {
            gr_bottom.visibility = View.GONE
        } else {
            gr_bottom.visibility = View.VISIBLE
        }
    }

    override fun onPageScrollStateChanged(p0: Int) {
    }

    override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
    }

    override fun onPageSelected(p0: Int) {
        when (p0) {
            0 -> rb_zixuan.isChecked = true
            1 -> rb_fenlei.isChecked = true
            2 -> rb_hanqing.isChecked = true
            3 -> rb_my.isChecked = true
        }
    }

    private var firstTime: Long = 0

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.rb_zixuan -> {
                vfpMain.currentItem = 0
                homeFragment.lazyLoad()
            }
            R.id.rb_hanqing -> vfpMain.currentItem = 2
            R.id.rb_my -> vfpMain.currentItem = 3
            R.id.rb_fenlei -> vfpMain.currentItem = 1
        }
    }

    override fun onKeyDown(paramInt: Int, paramKeyEvent: KeyEvent): Boolean {
        if (paramInt == KeyEvent.KEYCODE_BACK && paramKeyEvent.action == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - firstTime > 2000) {
                toast(R.string.msg_app_exit)
                firstTime = System.currentTimeMillis()
            } else {
                finish()
                myApplicaton?.exitApp()
            }
            return true
        }
        return super.onKeyDown(paramInt, paramKeyEvent)
    }
}